package com.libreaccess.reporting.scheduler;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

import com.libreaccess.reporting.scheduler.SchedulerManager;
import com.libreaccess.reporting.utils.Logger;

public class QuartzServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	final Logger logger = new Logger(getClass());

	/*
	 * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
	 * @param ServletConfig config
	 * @return void
	 */
	public void init(ServletConfig config) throws ServletException {
		logger.debug("Quartz Initializer Servlet loaded, initializing Scheduler...");
		super.init(config);
		SchedulerManager.init();
	}

	public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
	}

	public String getServletInfo() {
		return null;
	}

	/* 
	 * @return void
	 */
	public void destroy() {
		try {
			if (SchedulerManager.scheduler != null) {
				SchedulerManager.scheduler.shutdown();
				SchedulerManager.scheduler = null;
				SchedulerManager.factory = null;
				SchedulerManager.instance = null;
			}
		} catch (Exception e) {
			logger.error("Quartz Scheduler failed to shutdown cleanly: " + e.getMessage());
		}
		logger.debug("Quartz Scheduler successful shutdown.");
	}
}
